package com.kingdee.shr.affair.web.handler;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.hr.affair.ResignItemEntryFactory;
import com.kingdee.eas.hr.affair.ResignItemEntryInfo;
import com.kingdee.eas.hr.affair.ResignItemFactory;
import com.kingdee.eas.hr.affair.ResignItemInfo;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;

public class ResignItemEditHandler extends EditHandler {
	public String getResignItemAction(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		String bill = request.getParameter("bill");
		String type = request.getParameter("item");
		try {
			ResignItemInfo item = ResignItemFactory.getRemoteInstance()
					.getResignItemInfo(" where bill.id='" + bill + "'");
			ResignItemEntryInfo entry = ResignItemEntryFactory
					.getRemoteInstance().getResignItemEntryInfo(
							" where parent.id='" + item.getId()
									+ "' and item='" + type + "'");
			Map map = new HashMap();
			map.put("money", entry.getMoney());
			map.put("context", entry.getContext());
			if (entry.isIsFinish()) {
				map.put("confirm1", entry.isConfirm1());
				map.put("confirm2", entry.isConfirm2());
				map.put("isInvolve1", entry.getIsInvolve1());
				map.put("isInvolve2", entry.getIsInvolve2());
			} else {
				map.put("confirm1", "");
				map.put("confirm2", "");
				map.put("isInvolve1", "");
				map.put("isInvolve2", "");
			}

			map.put("itemid", entry.getId());
			JSONUtils.writeJson(response, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String updateResignItemAction(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		String json = request.getParameter("json");
		try {
			Map<String, Object> map = JSONObject.parseObject(json, Map.class);
			ResignItemEntryInfo entry = ResignItemEntryFactory
					.getRemoteInstance().getResignItemEntryInfo(
							" where id='" + map.get("itemid") + "'");
			entry
					.setConfirm1(map.get("confirm1").toString().equals("1") ? true
							: map.get("confirm1").toString().equals("0") ? false
									: null);
			entry
					.setConfirm2(map.get("confirm2").toString().equals("1") ? true
							: map.get("confirm2").toString().equals("0") ? false
									: null);
			entry
					.setIsInvolve1(map.get("isInvolve1").toString().equals("") ? -1
							: Integer.valueOf(map.get("isInvolve1").toString()));
			entry
					.setIsInvolve2(map.get("isInvolve1").toString().equals("") ? -1
							: Integer.valueOf(map.get("isInvolve2").toString()));
			entry.setContext(map.get("context").toString());
			entry.setMoney(new BigDecimal(map.get("money").toString()));
			entry.setIsFinish(true);
			ResignItemEntryFactory.getRemoteInstance().update(
					new ObjectUuidPK(entry.getId()), entry);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
